/*
 * marketsim-eclipsetrader
 *
 * Copyright (C) 2008-2011 Steve Phelps and John Cartlidge
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 */
/*
 *  (C) Victria Ltd. 2007
 */
/*
 * Copyright (c) 2004-2006 Marco Maccaferri and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Marco Maccaferri - initial API and implementation
 */

package victria.marketsim.eclipsetrader.action;

import net.sourceforge.eclipsetrader.core.ui.views.EventsView;

import org.apache.log4j.Logger;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;

import victria.marketsim.bot.BotContainer;
import victria.marketsim.bot.event.BotContainerStartedEvent;
import victria.marketsim.bot.event.BotContainerStoppedEvent;
import victria.marketsim.bot.event.BotEvent;
import victria.marketsim.bot.event.BotEventListener;

import victria.marketsim.core.BeanFactorySingleton;

public class StopBotsAction implements IWorkbenchWindowActionDelegate,
		IViewActionDelegate, BotEventListener {

	static Logger logger = Logger.getLogger(StopBotsAction.class);

	protected BotContainer botContainer;
	
	protected IAction action;
	
	public StopBotsAction() {
		super();
		botContainer = (BotContainer) BeanFactorySingleton
				.getBean("botContainer");
		botContainer.addListener(this);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#init(org.eclipse.ui.IWorkbenchWindow)
	 */
	public void init(IWorkbenchWindow window) {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IViewActionDelegate#init(org.eclipse.ui.IViewPart)
	 */
	public void init(IViewPart view) {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IWorkbenchWindowActionDelegate#dispose()
	 */
	public void dispose() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IActionDelegate#run(org.eclipse.jface.action.IAction)
	 */
	public void run(IAction action) {
//		try {	
			botContainer.stopBots();
			
//			IWorkbenchPage activePage = PlatformUI.getWorkbench()
//					.getActiveWorkbenchWindow().getActivePage();
//			IViewPart eventsView = activePage.showView(EventsView.VIEW_ID);
			
//		} catch (PartInitException e) {
//			throw new DuvacyRuntimeException(e);
//		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.IActionDelegate#selectionChanged(org.eclipse.jface.action.IAction,
	 *      org.eclipse.jface.viewers.ISelection)
	 */
	public void selectionChanged(IAction action, ISelection selection) {
		if (botContainer != null) {
			action.setEnabled(botContainer.isRunning());
			this.action = action;
		}
	}

	public void eventOccurred(BotEvent event) {
		if (event instanceof BotContainerStartedEvent) {
			if (action != null) {
				action.setEnabled(true);
			}
		} else if (event instanceof BotContainerStoppedEvent) {
			if (action != null) {
				action.setEnabled(false);
			}
 		}
	}
	
	
}
